import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectClassDemo {


    //1.通过反射创建对象
    public static void reflectNewInstance() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class<?> c1 = Class.forName("Student");//路径

        Student student = (Student) c1.newInstance();//向上转型

        System.out.println(student);
    }


    public static void reflectPrivateConstructor()  {


        try {
            Class<?> c1 = Class.forName("Student");//路径
            Constructor<?> constructor = c1.getDeclaredConstructor(String.class,int.class);

            //获取的是私有的，通过下面的方法，之后在类里可以进行更改
            constructor.setAccessible(true);

            Student student = (Student)constructor.newInstance("hanqing",18);

            System.out.println(student);


        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

    public static void reflectPrivateField(){

        try {
            Class<?> c1 = Class.forName("Student");//路径
            Student student = (Student) c1.newInstance();//向上转型

            Field field = c1.getDeclaredField("name");
            field.setAccessible(true);

            //开始修改
            field.set(student,"abo");
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

    public static void reflectPrivateMethod(){
        try {
            Class<?> c1 = Class.forName("Student");//路径
            Student student = (Student) c1.newInstance();//向上转型

            Method method = c1.getDeclaredMethod("function",String.class);

            method.setAccessible(true);

            method.invoke(student,"我是一个参数");
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }



    public static void main(String[] args) {

        reflectPrivateConstructor();
        reflectPrivateField();
        reflectPrivateMethod();

    }

    public static void main1(String[] args)  {

        //处理异常
        try {
            reflectNewInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
